package com.pojoin.cms.service.impl;

import java.util.List;


import com.pojoin.cms.dao.UserDao;
import com.pojoin.cms.domain.User;
import com.pojoin.cms.service.UserService;
import com.pojoin.cms.util.criteria.Criteria;
import com.pojoin.cms.util.criteria.CriteriaOperator;
import com.pojoin.cms.util.criteria.impl.CriteriaKeyValue;
import com.pojoin.cms.util.criteria.orderby.Orderby;
import com.pojoin.cms.util.criteria.orderby.OrderbyCost;
import com.pojoin.cms.util.pager.Pager;

public class UserServiceImpl implements UserService {
	
	private UserDao userDao;
	

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public List<User> getAllUsers(Pager pager) {
//		Criteria c = new CriteriaKeyValue("deleted", CriteriaOperator.EQUALS, 0);
		Orderby orderby = new Orderby("userId", OrderbyCost.DESC);
		return this.userDao.search(null, orderby, pager);
	}

	@Override
	public User getUserByUserName(String userName) {
		Criteria c = new CriteriaKeyValue("userName", CriteriaOperator.EQUALS, userName);
		List<User> list =  this.userDao.search(c, null, null);
		if(list != null && list.size() >0 ){
			return list.get(0);
		}
		return null;
	}

	@Override
	public User addUser(User user) {
		if(this.isExistUser(user.getUserName())){
			return null;
		}
		return this.userDao.add(user);
	}

	private boolean isExistUser(String userName) {
		if(this.userDao.checkUserByUserName(userName)){
			return true;
		}
		return false;
	}

	@Override
	public User fingById(Long userId) {
		return this.userDao.findById(userId);
	}

}
